    #include <QMessageBox>

#include "kdatabase.h"
#include "kongm.h"

kDatabase::kDatabase()
{
    _DEBUG_
    ikon = QSqlDatabase::addDatabase("QSQLITE");
    ikon.setDatabaseName( "kongm.kdb" );

    isOk = false;
    checkDatabases();
    createBackup();
}

bool kDatabase::checkDatabases()
{
    if( !ikon.open() )
    {
        if( isOk )
        {
            qDebug() << "Unable to establish a database connection.\n"
                "KON Game Manager needs SQLite support. Please read\n"
                "KON documentation for more information.";

            return false;
        }
        else
        {
            isOk = true;
            createInternalDatabase();
        }
    }

    if( !ikon.tables(QSql::Tables).contains( "iSystem" ))
    {
        createInternalDatabase();
    }
}

bool kDatabase::createInternalDatabase()
{
    QSqlQuery query;

    query.exec("create table iSystem ( major int, minor int, date varchar(20))");
    qDebug() << "Creating table iSystem." << query.lastError().databaseText();

    query.exec( QString( "insert into iSystem( major, minor, date) VALUES (%1, %2, %3 )" ).arg(KON_MAJOR_VERSION).arg(KON_MINOR_VERSION).arg(QDate::currentDate().toString("dd.MM.yyyy")));
    qDebug() << "Inserting data into iSystem";

    query.exec("create table games (id int primary key, gid int(10), profile int(5), name varchar(256), path TEXT, savepath TEXT, lupdate varchar(32), autosync int(1), upload int(1))");
    qDebug() << "Creating table games." << query.lastError().databaseText();

    query.exec("create table profiles (id int primary key, name varchar(64))");
    qDebug() << "Creating table profiles." << query.lastError().databaseText();

    return false;
}

/*
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("kongm.kdb");


    if (!db.open()) {
        qDebug() << "Unable to establish a database connection.\n"
                "This example needs SQLite support. Please read "
                "the Qt SQL driver documentation for information how "
                "to build it.";
         return -1;
     }

    if( !db.tables(QSql::Tables).contains( "games" ))
    {
        QSqlQuery query;
        query.exec("create table games (id int INCREMENTAL primary key, int profile int(5), name varchar(64), path TEXT, savepath TEXT, lupdate varchar(32), autosync int(1), upload int(1))");
        qDebug() << "Creating table games." << query.lastError().databaseText();
    }

    if( !db.tables(QSql::Tables).contains( "profiles" ))
    {
        QSqlQuery query;
        query.exec("create table profiles (id int primary key, name varchar(64))");
        qDebug() << "Creating table profiles." << query.lastError().databaseText();
    }
*/

bool kDatabase::createBackup()
{
    QFile mdb_kon("kongm.kdb");
    QFile mdb_games("games.kdb");

    if( !mdb_kon.exists() )
    {
        qDebug() << "KON Database not found. Can't make a backup\n"
                "Please check documentation for more info.";
        return false;
    }

/*    if( !mdb_games.exists() )
    {
        qDebug() << "KON Games Database not found. Can't make a backup\n"
                "Please check documentation for more info.";
        return false;
    }
*/


}
